﻿
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using StorageApp.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Drawing.Printing;
using gregn6Lib;

namespace StorageApp
{
    internal class PrintBarCode
    {
        private GridppReport Report = new GridppReport(); 
        private string lable_name = string.Empty;
        private string PrintName = string.Empty;
        private string labelData = string.Empty;
        public string Lable_name
        {
            get { return lable_name; }
            set { lable_name = value; }
        }
 
        private int printType = 0;

        public int PrintType
        {
            get { return printType; }
            set { printType = value; }
        }

        private string barcodeString = string.Empty;
        public string BarcodeString
        {
            get { return barcodeString; }
            set { barcodeString = value; }
        }
         
        public PrintBarCode(string reportStructure, string reportData,string printname)
        {  
            loadGrf(reportStructure.ToString());
            labelData = reportData;

            Report.Initialize += new _IGridppReportEvents_InitializeEventHandler(ReportInitialize);
            PrintName = Method.IniReadValue(printname);  //打印机名称
            if (PrintName == null|| PrintName=="") { 
                PrintDocument printDocument = new PrintDocument();
                PrintName = printDocument.PrinterSettings.PrinterName;
                Method.IniWrite(printname, PrintName);
            } 
            Report.Printer.Copies = 1; 
        }
        public void loadGrf(string xml)
        {
            Report.LoadFromStr(xml);
        }

        private void ReportInitialize()
        {
            JObject data = JObject.Parse(labelData);
            JObject main = new JObject();
            main["xml"] = data;
            XmlDocument doc = JsonConvert.DeserializeXmlNode(main.ToString());
            Report.DetailGrid.Recordset.LoadDataFromXML(doc.OuterXml);
        }


        private void PrintBar_Load(object sender, EventArgs e)
        {

        }
        public void Print(int copies )
        {
            Report.Printer.PrinterName = PrintName;
            Report.Printer.Copies= copies;
            Report.Print(false);
        }
        public void Print(bool showDialog = false)
        {
            Report.Printer.PrinterName = PrintName;
            Report.Print(showDialog);
        }
        public void PrintView()
        {
            Report.PrintPreview(true);
        }

    }
}
